﻿using System;
using System.Collections.Generic;

namespace Hanhud
{
    public class CTypeCollection<TYPE> : IEnumerable<TYPE> where TYPE: class
    {
        class CTypeEnumerator<T> : IEnumerator<T>
        {
            private T[] m_pT;
            private int m_nPosition;

            public CTypeEnumerator(T[] pT)
            {
                m_pT = pT;

                m_nPosition = -1;
            }

            #region IEnumerator<T> Members

            public T Current
            {
                get
                {
                    if (m_pT == null || m_pT.Length == 0 || m_nPosition == -1 || m_nPosition >= m_pT.Length)
                        throw new IndexOutOfRangeException();
                    return m_pT[m_nPosition];
                }
            }

            #endregion

            #region IDisposable Members

            public void Dispose()
            {

            }

            #endregion

            #region IEnumerator Members

            object System.Collections.IEnumerator.Current
            {
                get
                {
                    if (m_pT == null || m_pT.Length == 0 || m_nPosition == -1 || m_nPosition >= m_pT.Length)
                        throw new IndexOutOfRangeException();
                    return m_pT[m_nPosition];
                }
            }

            public bool MoveNext()
            {
                if (m_pT != null && m_pT.Length > 0)
                {
                    m_nPosition++;
                    return m_nPosition < m_pT.Length;
                }
                else return false;
            }

            public void Reset()
            {
                m_nPosition = -1;
            }

            #endregion
        }
        private TYPE[] m_pT;
        private int m_nCount;
        private int m_nTotalCount;

        public int Count
        {
            get { return m_nCount; }
        }

        public int TotalCount
        {
            get { return m_nTotalCount; }
            set { m_nTotalCount = value; }
        }

        public TYPE this[int index]
        {
            get
            {
                if (index >= m_nCount || index < 0)
                    throw new IndexOutOfRangeException();
                return m_pT[index];
            }
            set
            {
                if (index >= m_nCount || index < 0)
                    throw new IndexOutOfRangeException();
                m_pT[index] = value;
            }
        }

        public CTypeCollection(TYPE[] pT)
        {
            if (pT != null && pT.Length > 0)
            {
                m_pT = new TYPE[pT.Length];
                for (int i = 0; i < pT.Length; i++)
                    m_pT[i] = pT[i];

                m_nCount = pT.Length;
            }
            else m_nCount = 0;

            m_nTotalCount = m_nCount;
        }

        public CTypeCollection(TYPE[] pT, int totalCount)
            : this(pT)
        {
            m_nTotalCount = totalCount;
        }

        #region IEnumerable<T> Members

        public IEnumerator<TYPE> GetEnumerator()
        {
            return new CTypeEnumerator<TYPE>(m_pT);
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new CTypeEnumerator<TYPE>(m_pT);
        }

        #endregion
    }
}
